/**
 * @description 计算一个数的约数之和
 * @author dongling.li
 * @date 2021-04-14
 * @param {number} num 需要计算约数之和的数字
 */
function getApproxNum(num) {
    let count = 0;
    for (let i = 1; i < num; i++) {
        if (num % i === 0) {
            count += i;
        }
    }
    return count;
}

/**
 * @description 获取阶乘
 * @author dongling.li
 * @date 2021-04-14
 * @param {number} num 需要计算阶乘的数字
 */
 function getFactorial(num) {
    let count = 1;
    for (let i = 1; i <= num; i++) {
        count *= i;
    }
    return count;
}

/**
 * @description 获取num以内的所有质数
 * @author dongling.li
 * @date 2021-04-14
 * @param {*} num
 */
function getPrimeNumber(num) {
    let arr = [1];
    for (let i = 1; i < num; i++) {
        let zeroCount = 0;      //0的个数
        for (let j = 1; j <= i; j++) {
            if (i % j === 0) {
                zeroCount++;
            }
        }
        // 只有1和本身
        if (zeroCount === 2) {
            arr.push(i);
        }
    }
    return arr;
}

/**
 * @description 反转字符串
 * @author dongling.li
 * @date 2021-04-14
 * @param {string} str 需要反转的字符串
 */
function reverseStr(str) {
    let arr = Array.from(str);
    arr.reverse();
    return arr.join('');
}

/**
 * @description 获取input输入框数据，判断输入数字是否是完美数
 * @author dongling.li
 * @date 2021-04-14
 * @returns {*} 
 */
function getPerfectNum() {
    let text = document.getElementById('num');
    let num = text.value;
    let answer = document.getElementById('answer_1');
    if (!/^[0-9]*$/i.test(num)) {
        answer.innerHTML = "输入错误：请输入正确的数字";
        answer.style.color = "#f63517";
        return;
    }
    num = parseInt(num);
    if (num === 0) {
        answer.innerHTML = "输入错误：请输入有效的数字";
        answer.style.color = "#f63517";
        return;
    }
    let count = getApproxNum(num);
    answer.style.color = "#333";
    if (count === num) {
        answer.innerHTML = `解析：${num}是一个完美数`;
    } else {
        answer.innerHTML = `解析：${num}不是一个完美数`;
    }
}

/**
 * @description 获取1~100的盈数
 * @author dongling.li
 * @date 2021-04-14
 */
function getAbundantNum() {
    let arr = [];
    //由于最小的盈数是12，则12之前的数不作判断
    for (let i = 12; i <= 100; i++) {
        let count = 0;
        for (let j = 1; j < i; j++) {
            count += j;
        }
        if (count > i) {
            arr.push(i);
        }
    }
    let answer = document.getElementById('answer_2');
    let str = arr.join(', ');
    answer.innerHTML = '1~100的盈数为：<br>' + str;
}

/**
 * @description 计算阶乘
 * @author dongling.li
 * @date 2021-04-14
 */
function caculateFactorial() {
    let text = document.getElementById('num_3');
    let num = text.value;
    let answer = document.getElementById('answer_3');
    if (!/^[0-9]*$/i.test(num)) {
        answer.innerHTML = "输入错误：请输入正确的数字";
        answer.style.color = "#f63517";
        return;
    }
    num = parseInt(num);
    if (num === 0) {
        answer.innerHTML = "输入错误：请输入有效的数字，x>=1";
        answer.style.color = "#f63517";
        return;
    }
    let count = 0;
    for (let i = 1; i <= num; i++) {
        count += getFactorial(i);
    }
    answer.style.color = "#333";
    answer.innerHTML = `解析：1-${num}阶乘之和=${count}`;
}

/**
 * @description 获取100~999的喇叭花数
 * @author dongling.li
 * @date 2021-04-14
 */
function getMorningGlory() {
    let arr = [];
    for (let i = 100; i <= 999; i++) {
        let str = i.toString();
        let count = getFactorial(parseInt(str[0])) + getFactorial(parseInt(str[1])) + getFactorial(parseInt(str[2]));
        // for (let j = 0, len = str.length; j < len; j++) {
        //     count += getFactorial(parseInt(str[j]));
        // }
        if (count === i) {
            arr.push(i);
        }
    }
    let answer = document.getElementById('answer_4');
    answer.style.color = "#333";
    answer.innerHTML = `解析：100~999的喇叭花数为${arr.join(', ')}`;
}

/**
 * @description 获取2000以内的亲密数
 * @author dongling.li
 * @date 2021-04-14
 */
function getDearNumber() {
    let arr = [];
    let temp = {};
    for (let i = 2; i <= 2000; i++) {
        let n = getApproxNum(i);
        if (temp[n] && temp[n] === i) {
            arr.push(`(${n}和${i})`)
        }
        temp[i] = n;
    }
    let answer = document.getElementById('answer_5');
    answer.style.color = "#333";
    answer.innerHTML = `解析：2000以内的亲密数为${arr.join(', ')}`;
}

/**
 * @description 拆分偶数为两个质数之和
 * @author dongling.li
 * @date 2021-04-14
 */
function splitDouble() {
    let text = document.getElementById('num_6');
    let num = text.value;
    let answer = document.getElementById('answer_6');
    if (!/^[0-9]*$/i.test(num)) {
        answer.innerHTML = "输入错误：请输入正确的数字";
        answer.style.color = "#f63517";
        return;
    }
    num = parseInt(num);
    if (num < 2) {
        answer.innerHTML = "输入错误：请输入有效的数字，x>=2";
        answer.style.color = "#f63517";
        return;
    }

    let primeNumArr = getPrimeNumber(num);
    let isFind = false;
    for (var i = 0, len = primeNumArr.length; i < len; i++) {
        let sum = 0;
        for (var j = 0; j < len; j++) {
            sum = primeNumArr[i] + primeNumArr[j];
            if (sum === num) {
                isFind = true;
                break;
            }
        }
        if (isFind) {
            break;
        }
    }

    answer.style.color = "#333";
    answer.innerHTML = `解析：${num}=${primeNumArr[i]}+${primeNumArr[j]}`;
}

/**
 * @description 反转字符串
 * @author dongling.li
 * @date 2021-04-14
 */
function rev() {
    let text = document.getElementById('num_7');
    let str = text.value;
    let answer = document.getElementById('answer_7');
    // let strlen = str.length;
    // let curArr = Array.from(str);
    // for (let i = 0, len = Math.floor(strlen / 2); i < len; i++) {
    //     [curArr[i], curArr[strlen - i - 1]] = [curArr[strlen - i - 1], curArr[i]];
    // }
    let reStr = reverseStr(str);
    answer.style.color = "#333";
    answer.innerHTML = `解析：倒置后结果为"${reStr}"`;
}

/**
 * @description 反转beijing
 * @author dongling.li
 * @date 2021-04-14
 */
function reversBeiJing() {
    let answer = document.getElementById('answer_8');
    answer.style.color = "#333";
    let str = 'i come from beijing';
    let strArr = str.split(' ');
    strArr.reverse();
    answer.innerHTML = `解析：倒置后结果为"${strArr.join(' ')}"`;
}

/**
 * @description 右移字符串
 * @author dongling.li
 * @date 2021-04-14
 * @param {*} char 需要右移的字符串
 * @param {*} n 右移的位数
 */
function rightchange(char, n) {
    let answer = document.getElementById('answer_9');
    if (!/^[0-9]*$/i.test(n)) {
        answer.innerHTML = "输入错误：请输入正确的右移位数（正整数）";
        answer.style.color = "#f63517";
        return;
    }
    let headStr = char.slice(char.length - parseInt(n));
    let tailStr = char.slice(0, char.length - parseInt(n));
    answer.style.color = "#333";
    answer.innerHTML = `解析：右移${n}位后结果为"${headStr.concat(tailStr)}"`;
}

/**
 * @description 寻找最大重复子串
 * @author dongling.li
 * @date 2021-04-14
 * @param {*} char 字符串
 * @returns {*} 
 */
function maxr(char) {
    let answer = document.getElementById('answer_10');
    if (!char) {
        answer.innerHTML = "输入错误：请输入有效的字符串";
        answer.style.color = "#f63517";
        return;
    }

    //所有连续子串存到数组中
    let arr = [[]];
    let index = 0;
    let curWord = char[0];
    for (let i = 0, len = char.length; i < len; i++) {
        let str = char[i];
        if (str !== curWord) {
            curWord = str;
            arr.push([]);
            index++;
        }
        arr[index].push(str);
    }
    //从所有连续子串判断最长的，并记录
    let max = arr[0].length;
    let maxArr = arr[0];
    for (let j = 1, len = arr.length; j < len; j++) {
        if (arr[j].length > max) {
            max = arr[j].length;
            maxArr = arr[j];
        }
    }
    answer.style.color = "#333";
    answer.innerHTML = `解析：${char}最长重复子串为"${maxArr.join('')}"`;
}

/**
 * @description 平均成绩
 * @author dongling.li
 * @date 2021-04-14
 */
function averageScore() {
    let arr = [30, 50, 25, 66, 89, 95, 70, 45, 22, 84];
    let answer = document.getElementById('answer_11');
    let sum = 0;
    for (let i = 0, len = arr.length; i < len; i++) {
        sum += arr[i];
    }
    let average = (sum / 10).toFixed(2);
    let count = 0;
    for (let i = 0, len = arr.length; i < len; i++) {
        if (arr[i] >= average) {
            count++;
        }
    }
    answer.style.color = "#333";
    answer.innerHTML = `解析：10个人成绩为${arr}<br>平均成绩为：${average}，大于平均成绩的人数为：${count}`;
}

/**
 * @description 将首字母大写
 * @author dongling.li
 * @date 2021-04-14
 * @param {*} str
 */
function toUpperWord(str) {
    let arr = str.split(' ');
    for (let i = 0, len = arr.length; i < len; i++) {
        arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
    }
    let answer = document.getElementById('answer_12');
    answer.style.color = "#333";
    answer.innerHTML = `解析：结果${arr.join(' ')}`;
}

/**
 * @description 获取最大值及最小值
 * @author dongling.li
 * @date 2021-04-14
 */
function getMaxAndMin() {
    let arr = [30, 50, 25, 66, 89, 95, 70, 45, 22, 84];
    let maxNum = arr[0];
    let minNum = arr[0];

    let maxIndex = 0;
    let minIndex = 0;

    for (let i = 1, len = arr.length; i < len; i++) {
        if (maxNum < arr[i]) {
            maxNum = arr[i];
            maxIndex = i;
        }
        if (minNum > arr[i]) {
            minNum = arr[i];
            minIndex = i;
        }
    }

    let answer = document.getElementById('answer_13');
    answer.style.color = "#333";
    answer.innerHTML = `解析：该组数为${arr}<br>最大值：${maxNum}，下标${maxIndex}，最小值：${minNum}，下标${minIndex}`;
}

/**
 * @description 用符号连接数组
 * @author dongling.li
 * @date 2021-04-14
 */
function concatArr() {
    let arr = [30, 50, 25, 66, 89, 95, 70, 45, 22, 84];

    let str = arr.join('|');
    let answer = document.getElementById('answer_14');
    answer.style.color = "#333";
    answer.innerHTML = `解析：用 | 连接后的字符串为：${str}`;
}

/**
 * @description 移除为0的数
 * @author 小A
 * @date 2021-04-14
 */
function removeZero() {
    let arr = [30, 0, 25, 0, 89, 95, 70, 0, 22, 84, 0, 2, 0];
    let newArr = arr.slice(0);
    // for (let i = 0, len = arr.length; i < len; i++) {
    //     if (arr[i]) {
    //         newArr.push(arr[i]);
    //     }
    // }
    for (let i = 0; i < newArr.length; i++) {
        if (!newArr[i]) {
            newArr.splice(i, 1);
            i--;
        }
    }
    let answer = document.getElementById('answer_15');
    answer.style.color = "#333";
    answer.innerHTML = `解析：原数组${arr}<br>去掉0后的数组：${newArr}`;
}

/**
 * @description 数组去重
 * @author dongling.li
 * @date 2021-04-14
 */
function removeRepeat() {
    let arr = [1,1,1,2,2,3,4,5,6,3,4,2,4,1,1];
    let n = new Set(arr);
    let newArr = Array.from(n);
    let answer = document.getElementById('answer_16');
    answer.style.color = "#333";
    answer.innerHTML = `解析：原数组${arr}<br>去重后的数组：${newArr}`;
}

/**
 * @description 数组排序，从小到大
 * @author dongling.li
 * @date 2021-04-14
 */
function sortArr() {
    let arr = [3,4,1,2,6,5];
    arr.sort((a, b) => {
        return a - b;
    });
    let answer = document.getElementById('answer_17');
    answer.style.color = "#333";
    answer.innerHTML = `解析：排序后数组：${arr}`;
}

/**
 * @description 数组转对象
 * @author dongling.li
 * @date 2021-04-14
 */
function arrayToObject() {
    let arr = ['a','a','a','b','b','c','c','c','c','d','d','d','d'];
    let obj = {};
    for (let i = 0, len = arr.length; i < len; i++) {
        if (!obj[arr[i]]) {
            obj[arr[i]] = 0;
        }
        obj[arr[i]] ++;
    }
    let answer = document.getElementById('answer_18');
    answer.style.color = "#333";
    answer.innerHTML = `解析：转换后对象：${JSON.stringify(obj)}`;
}

/**
 * @description 99乘法表
 * @author dongling.li
 * @date 2021-04-14
 */
function nineNine() {
    let arr = [];
    for (let i = 1; i <= 9; i++) {
        for (let j = i; j <= 9; j++) {
            arr.push(`${i}x${j}=${i*j} `);
        }
    }
    let answer = document.getElementById('answer_19');
    answer.style.color = "#333";
    answer.innerHTML = `解析：99乘法表：${arr.join('')}`;
}

/**
 * @description 最大利润
 * @author dongling.li
 * @date 2021-04-14
 */
function maxProfit() {
    let arr = [7, 1, 5, 3, 6, 4];
    let count = 0;
    let len = arr.length;
    let startIndex = 0;
    let min = arr[0];
    while(startIndex < len) {
        if (arr[startIndex] < min) min = arr[startIndex];
        if (count < arr[startIndex] - min) {
            count = arr[startIndex] - min;
        }
        startIndex++;
    }
    let answer = document.getElementById('answer_20');
    answer.style.color = "#333";
    answer.innerHTML = `解析：最大利润：${count}`;
}